package persistent.prestige.web.demo.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import persistent.prestige.web.demo.dao.CallProcedureDao;
import persistent.prestige.web.demo.dao.OrderDao;
import persistent.prestige.web.demo.model.Order;
import persistent.prestige.web.demo.model.OrderItem;
import persistent.prestige.web.demo.service.DemoService;

@Service("demoService")
@SuppressWarnings({ "rawtypes", "unchecked", "serial" })
public class DemoServiceImpl implements DemoService {
	
	@Autowired
	private CallProcedureDao callProcedureDao;
	
	@Autowired
	private OrderDao orderDao;
	
	/**
	 * 
	 */
	@Override
	public Map saveQryAndSaveOrder() {
		Map result = new HashMap();
		
		List<Order> orders = new ArrayList<Order>();
		Order o1 = new Order();
		o1.setId(31);
		Order o2 = new Order();
		o2.setId(33);
		orders.add(o1);
		orders.add(o2);
				//orderDao.findOrders("faeee492dbe511e7aff6c85b763dfer4");
		if(orders == null || orders.isEmpty()) {
			result.put("msg", "order is empty");
			return result;
		}
		for(Order o : orders ) {
			o.setStatus(1);
			orderDao.updateOrder(o);
			//再模拟查询
			orderDao.findOrders("faeee492dbe511e7aff6c85b763dfer4");
		}
		
		result.put("msg", "success");
		return result;
	}

	@Override
	public Map testSelectOrder(String uid) {
		// TODO Auto-generated method stub
		Map  result = new HashMap();
		result.put("data", orderDao.findOrders(uid));
		result.put("count", orderDao.countOrder());
		return result;
	}

	@Override
	public Map testCallProc(Integer id) {
		
		Map result = new HashMap();
		
		result.put("goodsId", callProcedureDao.testCallProc(id));
		return result;
	}

	@Override
	public Map saveTestERInsert() {
		
		Order o = new Order();
		o.setCreateTime(new Date(System.currentTimeMillis()));
		o.setCreator(1);
		o.setCreatorUid("1");
		o.setOrderNo("" + o.getCreateTime().getTime() / 1000 ); //用时间，单位秒（只是测试用）
		o.setPhone("18630850330");
		o.setReciveAddressId(1);
		o.setReciveName("广州");
		o.setSellerId(22);
		o.setTotalPrice(2000l);
		o.setStatus(1);
		
		orderDao.addOrder(o);
		
		
		//创建两个订单项
		OrderItem item1 = new OrderItem();
		item1.setCreateTime(o.getCreateTime());
		item1.setCreator(o.getCreator());
		item1.setGoodsId(1);
		item1.setNum(1);
		item1.setOrderId(o.getId());
		item1.setPrice(1000l);
		item1.setSkuId(2);
		item1.setTotalPrice(1000l);
		
		
		OrderItem item2 = new OrderItem();
		item2.setCreateTime(o.getCreateTime());
		item2.setCreator(o.getCreator());
		item2.setGoodsId(2);
		item2.setNum(1);
		item2.setOrderId(o.getId());
		item2.setPrice(1000l);
		item2.setSkuId(3);
		item2.setTotalPrice(1000l);
		
		List<OrderItem> items = new ArrayList<OrderItem>(2);
		items.add(item1);
		items.add(item2);
		
		orderDao.addOrderItem(o.getId(), items);
		
		Map result = new HashMap();
		result.put("code", 0);
		return result;
	}
	
	
	
	

}
